//
//  arcticViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-15.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "arcticViewController.h"
#import "arcticView.h"
#import "ArchiveHandleSingle.h"
#import "DetailModel.h"
#import "DetailViewController.h"
#import "DetailWebView.h"

@interface arcticViewController ()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation arcticViewController

- (void)dealloc
{
    [_arctic release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        //返回当取到沙盒数据
        [[ArchiveHandleSingle archiveSingle] unachiveArray];
        NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificateAction) name:@"articleArray" object:nil];
        
        self.arctic = [[[arcticView alloc] initWithFrame: CGRectMake(0,89, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 89 - 44)]autorelease];
        
    }
    return self;
}

-(void)loadView{

    self.view = self.arctic;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //设置代理
    self.arctic.arcticTab.delegate = self;
    
    self.arctic.arcticTab.dataSource = self;
    
    
    //注册cell
    
    [self.arctic.arcticTab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell_id"];
    // Do any additional setup after loading the view.
}

//通知中心的方法,刷新页面
- (void)notificateAction
{
    [self.arctic.arcticTab reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //返回的cell数是路径中model类的个数
    if ([ArchiveHandleSingle archiveSingle].articleArray.count == 0) {
        return 1;
    }else{
        return [ArchiveHandleSingle archiveSingle].articleArray.count;
    }


}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath ];
    
    if ([ArchiveHandleSingle archiveSingle].articleArray.count == 0) {
        cell.textLabel.text = @"暂无收藏";
    }else{
        NSInteger count = [ArchiveHandleSingle archiveSingle].articleArray.count;
        DetailModel *m = [ArchiveHandleSingle archiveSingle].articleArray[count - indexPath.row - 1];
        NSLog(@"标题:%@",m.title);
        cell.textLabel.text = m.title;
        
    }
    return cell;

}

//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([ArchiveHandleSingle archiveSingle].articleArray.count == 0) {
        
        
    }else{
    
        DetailViewController *deVC = [[[DetailViewController alloc] init] autorelease];
        
        NSInteger t = [ArchiveHandleSingle archiveSingle].articleArray.count;
        DetailModel *m = [ArchiveHandleSingle archiveSingle].articleArray[t - indexPath.row - 1];
        deVC.docidStr = m.docid;
        
        [self.navigationController pushViewController:deVC animated:YES];
    
    }

}


//2.指定哪些行可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3.确定编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _artStyle;
}
//4.完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //删除数据
        NSLog(@"删除数据");
        
        //删除沙盒数据
        if ([ArchiveHandleSingle archiveSingle].articleArray.count == 0) {
            
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"您还未收藏" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [av show];
            [av release];
            
        }else{
            
            //删除数据
        //    [[ArchiveHandleSingle archiveSingle].articleArray removeObjectAtIndex:indexPath.row];
            
            NSInteger t = [ArchiveHandleSingle archiveSingle].articleArray.count;
//            DetailModel *m = [ArchiveHandleSingle archiveSingle].articleArray[t - indexPath.row - 1];
//            NSLog(@"%@",m.docid);
//            [[ArchiveHandleSingle archiveSingle] deleteDMValue:m];
            
            NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
            NSString *filePath = [documentPath stringByAppendingString:@"/article.plist"];
            
            [[ArchiveHandleSingle archiveSingle].articleArray removeObjectAtIndex:t - indexPath.row - 1];
            [NSKeyedArchiver archiveRootObject:[ArchiveHandleSingle archiveSingle].articleArray toFile:filePath];
            [self.arctic.arcticTab reloadData];
        }
        
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
